<template>
  <div class="main-content">
    <div class="card" style="padding: 20px">
      <div style="margin-bottom: 20px; font-size: 24px; font-weight: bold">我的评论</div>
      <div style="margin-bottom: 20px">
        <el-input v-model="data.content" prefix-icon="Search" style="width: 240px; margin-right: 10px" placeholder="请输入评论内容"></el-input>
        <el-button type="info" plain @click="load">查询</el-button>
        <el-button type="warning" plain style="margin: 0 10px" @click="reset">重置</el-button>
      </div>

      <div style="margin-bottom: 20px">
        <el-table stripe :data="data.commentList">
          <el-table-column prop="content" label="内容"></el-table-column>
          <el-table-column prop="createTime" label="评论发布时间"></el-table-column>
          <el-table-column prop="fid" label="评论的帖子">
            <template v-slot="scope">
              <a :href="'/front/postDetail?id=' + scope.row.postId" target="_blank">点击查看</a>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="100" fixed="right">
            <template v-slot="scope">
              <el-button type="danger" circle :icon="Delete" @click="del(scope.row.id)"></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-if="data.total">
        <el-pagination
            @current-change="load"
            @size-change="handleSizeChange"
            background layout="total, prev, pager, next, jumper, sizes"
            :page-sizes="[1, 3, 5, 10, 15, 20, 30]"
            :page-size="data.pageSize"
            v-model:current-page="data.pageNum"
            :total="data.total"/>
      </div>
    </div>

  </div>
</template>

<script setup>
  import {reactive} from "vue";
  import request from "@/utils/request.js";
  import {ElMessage, ElMessageBox} from "element-plus";
  import {Delete} from "@element-plus/icons-vue";


  const data = reactive({
    pageNum: 1,
    pageSize: 10,
    content: null,
    commentList: [],
    total: 0,
  })

  const load = () => {
    request.post('/comment/pageQuery/person', {
      pageNum: data.pageNum,
      pageSize: data.pageSize,
      content: data.content,
    }).then(res => {
      if (res.code === '200') {
        data.commentList = res.data?.list || []
        data.total = res.data?.total
      } else {
        ElMessage.error(res.msg)
      }
    })
  }

  const reset = () => {
    data.content = null
    load()
  }

  load()

  const del = (id) => {
    ElMessageBox.confirm('删除后数据无法恢复，您确定删除吗？', '删除确认', { type: 'warning' }).then(res => {
      request.delete('/comment/delete/' + id).then(res => {
        if (res.code === '200') {
          ElMessage.success("删除成功")
          load()
        } else {
          ElMessage.error(res.msg)
        }
      })
    }).catch(err => {
      console.error(err)
    })
  }

  const handleSizeChange = (newSize) => {
    data.pageSize = newSize
    data.pageNum = 1
    load()
  }

</script>